package edu.gzc.nio.nonblockingnio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.time.LocalDateTime;
import java.util.Scanner;

/**
 * @Description:
 * @Author: guozongchao
 * @Date: 2021/1/7 22:11
 */
public class Client {
    public static void main(String[] args) throws IOException {
        SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9898));

        //切换非阻塞模式
        sChannel.configureBlocking(false);

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        Scanner sc = new Scanner(System.in);
        //发送当前时间到服务端
        while (sc.hasNext()) {
            String s = sc.next();
            System.out.println(s);
            buffer.put((LocalDateTime.now().toString() + "\n" + s).getBytes());
            buffer.flip();
            sChannel.write(buffer);
            buffer.clear();
        }
        sChannel.close();
    }
}
